package com.controller;

import com.domain.Account;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *1.导入SpringMVC相关坐标                         pom.xml加入依赖
 * 2.配置SpringMVC核心控制器DispathcerServlet      web.xml中配置servlet，init-param中加入spring-mvc.xml
 * 3.创建Controller类和视图页面                     创建controller类，创建jsp页面
 * 4.使用注解配置Controller类中业务方法的映射地址    添加注解
 * 5.配置SpringMVC核心文件 spring-mvc.xml             在里面添加注解扫描
 * 6.客户端发起请求测试
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

//    public UserService getUserService() {
//        return userService;
//    }
//
//    public void setUserService(UserService userService) {
//        this.userService = userService;
//    }

    // 请求地址  http://localhost:8080/user/quick
    @RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username"})
    public String save() {
        System.out.println("Controller save running....");
        return "success";
    }

    // 请求地址  http://localhost:8080/user/quick
    @RequestMapping(value = "/quick1")
    public String save1() {
        System.out.println("Controller save running....");
        return "success";
    }

    // 请求地址  http://localhost:8080/user/login
    @RequestMapping(value = "/login")
    public String save2(HttpServletRequest request) {
        String username = request.getParameter("userName");
        System.out.println(username);
        return "success";
    }

    // 请求地址  http://localhost:8080/user/login1
    @RequestMapping(value = "/login1")
    public String save2(String userName) {//参数必须和jsp页面设置的相同
        System.out.println(userName);
        return "success";
    }

    // 请求地址  http://localhost:8080/user/login2
    @RequestMapping(value = "/login2")
    public String save2(User user, HttpServletRequest request, Model model) {
        request.setAttribute("hello", user.getPassword());
        model.addAttribute("user1", user);

        List<User> ls = new ArrayList<>();
        User u1 = new User();
        u1.setPassword("123");
        u1.setUserName("hello");
        ls.add(u1);
        request.setAttribute("list1", ls);

        return "success";
    }

    @RequestMapping("/conn")
    public String testConnection(HttpServletRequest request) {
        List<Account> us = userService.query();
        request.setAttribute("us", us);
        return "test";
    }

    //测试JSON数据类型
    @RequestMapping("/json")
    @ResponseBody
    public List<Account> testGetall() {
        List<Account> us = userService.query();
        return us;
    }
}
